package mvc.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import mvc.model.pojos.AddressBean;
import mvc.model.service.AddressService;
import mvc.model.service.impl.AddressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AddrServlet", urlPatterns = "/addr")

public class AddrServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Gson gson = new GsonBuilder().serializeNulls().create();
        String data = request.getParameter("addr");
        AddressBean address = gson.fromJson(data, AddressBean.class);
        AddressService addressService = new AddressServiceImpl();
        addressService.addAddress(address);
        response.getWriter().append(gson.toJson("添加成功"));

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

}
